﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Configuration;
using System.Net.Mail;
using System.Net;

namespace WuxiWTO.Web
{
    public partial class forgetPassword : System.Web.UI.Page
    {
        WTODataContext ctx = new WTODataContext();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender,EventArgs e)
        {
            this.btnLogin.Enabled = false;
            this.btnLogin.Text = "发送中...";
            supporter user = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == this.txtLoginName.Text.Trim() && p.email == this.txtEmail.Text.Trim());
            if (user != null)
            {
                string mailFrom = ConfigurationManager.AppSettings["FormEmail"].ToString();
                string mailPassword = ConfigurationManager.AppSettings["FormPWD"].ToString();
                string mailTo = this.txtEmail.Text.Trim();
                string port = ConfigurationManager.AppSettings["SMTPPort"].ToString();
                string subject = ConfigurationManager.AppSettings["EmailTitleGetPass"].ToString();
                string smtpServer = ConfigurationManager.AppSettings["MailSMTP"].ToString();
                bool enableSSL = false;
                int portNum = 25;
                bool.TryParse(ConfigurationManager.AppSettings["UseSSL"].ToString(), out enableSSL);
                int.TryParse(ConfigurationManager.AppSettings["SMTPPort"].ToString(), out portNum);
                string mailBody = string.Format("{0},您好：<br/>您在本中心注册的用户名为：{1},密码为：{2}.请牢记您的用户名和密码，不要告知他人.<br/>", user.enterprise_name, user.supporter_login_name, user.supporter_password);

                SmtpClient smtp = new SmtpClient();
                smtp.EnableSsl = enableSSL;
                smtp.Host = smtpServer;
                smtp.Port = portNum;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential(mailFrom, mailPassword);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                MailMessage msg = new MailMessage(mailFrom, mailTo, subject, mailBody);
                msg.BodyEncoding = System.Text.Encoding.UTF8;
                msg.IsBodyHtml = true;
                smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
                smtp.SendAsync(msg, msg.To);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "idf", "alert('您的用户名和Email地址不匹配,请重新输入!');", true);
                return;
            }
            this.btnLogin.Enabled = true;
            this.btnLogin.Text = "找回密码";
        }

        void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "cancel", "alert(\"邮件发送被取消\");", true);
            }
            if (e.Error == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert(\"成功发送邮件\");", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "exception", "alert(\"邮件发送失败，错误：" + e.Error.Message + "\");", true);
            }
        }
    }
}